/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Views.GestionRestaurateur;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 *
 * @author ibrahim
 */
public class RestaurateurPanel extends JPanel {
    public RestaurateurPanel(){
    Dimension dim =getPreferredSize();
    dim.width=250;
    setPreferredSize(dim);
    
    this.fNameLabel = new JLabel("Prenom: ");
    this.fNameLabel.setEnabled(false);
    this.lNameLabel = new JLabel("Nom: ");
    this.lNameLabel.setEnabled(false);
    this.fNameTextFeild = new JTextField(10);
    this.fNameTextFeild.setEnabled(false);
    this.lNameTextFeild = new JTextField(10);
    this.lNameTextFeild.setEnabled(false);
    this.submitButton = new JButton ("Ok");
    this.saveButton = new JButton ("Save");
    this.refreshButton = new JButton("Refresh");
    Dimension btnSize = refreshButton.getPreferredSize();
    this.submitButton.setPreferredSize(btnSize);
    this.submitButton.setEnabled(false);
    this.saveButton.setEnabled(false);
    this.saveButton.setPreferredSize(btnSize);
    this.expertiseList = new JList();
    this.expertiseList.setEnabled(false);
    this.citizenCheck = new JCheckBox();
    citizenCheck.setEnabled(false);
    this.permanantCodeField = new JTextField(10);
    this.permanantCodeLabel = new JLabel("Code Permanent: ");
    //Set up Permanent Code
    this.permanantCodeLabel.setEnabled(false);
    this.permanantCodeField.setEnabled(false);
    this.maleRadio = new JRadioButton("Male");
    this.femaleRadio = new JRadioButton("Femelle");
    this.genderGroup = new ButtonGroup();
    this.maleRadio.setEnabled(false);
    this.femaleRadio.setEnabled(false);
    this.maleRadio.setSelected(true);
    this.maleRadio.setActionCommand("male");
    this.femaleRadio.setActionCommand("femelle");
    // Set up Gender Group
    genderGroup.add(maleRadio);
    genderGroup.add(femaleRadio);
    
    //Set up Memonics
    this.submitButton.setMnemonic(KeyEvent.VK_O);
    
    //Set up Expertise List
    DefaultListModel expModel = new DefaultListModel();
    expModel.addElement(new Expertise(0,"Junior"));
    expModel.addElement(new Expertise(1,"Intermediate"));
    expModel.addElement(new Expertise(2,"Senior"));
    this.expertiseList.setModel(expModel);
    this.expertiseList.setPreferredSize(new Dimension(114,86));
    this.expertiseList.setBorder(BorderFactory.createEtchedBorder());
    this.expertiseList.setSelectedIndex(1);
    
    this.submitButton.addActionListener(new ActionListener() {

       
        public void actionPerformed(ActionEvent e) {
            String fname = fNameTextFeild.getText();
            String lname = lNameTextFeild.getText();
            Expertise expertise = (Expertise)expertiseList.getSelectedValue();
            boolean citizen = citizenCheck.isSelected();
            String permanantCode = permanantCodeField.getText();
            String gender = genderGroup.getSelection().getActionCommand();
            AddRestaurateurEvent arEv = new AddRestaurateurEvent(this,fname,lname,expertise.getID(),citizen,permanantCode,gender);
            if(arListener!=null){
                arListener.AddRestaurateurEventOccurred(arEv);
                fNameTextFeild.setText("");
                lNameTextFeild.setText("");
                permanantCodeField.setText("");
                
            }
        }
    });
    
    this.saveButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(sdbListener!=null){
                sdbListener.savetodatabaseEventOccured();
            }
        }
    });
    
    this.refreshButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            submitButton.setEnabled(true);
            saveButton.setEnabled(true);
            fNameLabel.setEnabled(true);
            fNameTextFeild.setEnabled(true);
            lNameLabel.setEnabled(true);
            lNameTextFeild.setEnabled(true);
            expertiseList.setEnabled(true);
            maleRadio.setEnabled(true);
            femaleRadio.setEnabled(true);
            citizenCheck.setEnabled(true);
            if(ldbListener!=null){
                ldbListener.loadfromdatabaseEventOccured();
            }
        }
    });
    
    this.citizenCheck.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            boolean isTicked = citizenCheck.isSelected();
            permanantCodeLabel.setEnabled(isTicked);
            permanantCodeField.setEnabled(isTicked);
            permanantCodeField.setText("");
        }
    });
    
    Border innerBorder = BorderFactory.createTitledBorder("Ajouter Restaurateur");
    Border outerBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
    setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));
    
    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();
    //First Row
    
    gc.gridy=0;
    gc.weightx=1;
    gc.weighty=0.1;
    gc.gridx=0;
    gc.fill = GridBagConstraints.NONE;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(this.fNameLabel,gc);
    gc.gridx=1;
    gc.gridy=0;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.fNameTextFeild,gc);
    //Second Row
    gc.gridy++;
    //gc.weightx=1;
   // gc.weighty=0.1;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(this.lNameLabel,gc);
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.lNameTextFeild,gc);       
    //Third Row
    gc.gridy++;
    gc.weightx=1;
    gc.weighty=0.2;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(new JLabel("Expertise: "),gc);
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.expertiseList,gc);
    //Fourth Row
    gc.gridy++;
    //gc.weightx=1;
   // gc.weighty=0.2;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(new JLabel("Citoyen Canadien: "),gc);
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.citizenCheck,gc);
    //Fifth Row
    gc.gridy++;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(this.permanantCodeLabel,gc);
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.permanantCodeField,gc);
    //Sixth Row
    gc.gridy++;
    gc.weightx=1;
    gc.weighty=0.02;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(new JLabel("Sexe: "),gc);
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.maleRadio,gc);
    //Seventh Row
    gc.gridy++;
    gc.weightx=1;
    gc.weighty=0.02;
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.femaleRadio,gc);
    //Eighth Row
    gc.gridy++;
    gc.weightx=1;
    gc.weighty=1;
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.submitButton,gc);
    //Nineth Row
    gc.weightx=1;
    gc.weighty=1;
    gc.gridy++;
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.saveButton,gc);
    //Last Row ///
    gc.weightx=1;
    gc.weighty=1;
    gc.gridy++;
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.refreshButton,gc);
    
    }
    public void setAddRestaurateurListener(AddRestaurateurListener arListener){
        this.arListener = arListener;
    }
    public void setSaveToDatabaseListener(SaveToDatabaseListener sdbListener){
        this.sdbListener = sdbListener;
    }
    public void setLoadFromDatabaseListener(LoadFromDatabaseListener ldbListener){
        this.ldbListener = ldbListener;
    }
   
    private JLabel fNameLabel;
    private JLabel lNameLabel;
    private JTextField fNameTextFeild;
    private JTextField lNameTextFeild;
    private JButton submitButton;
    private JButton saveButton;
    private JButton refreshButton;
    private AddRestaurateurListener arListener;
    private SaveToDatabaseListener sdbListener;
    private LoadFromDatabaseListener ldbListener;
    private JList expertiseList;
    private JCheckBox citizenCheck;
    private JTextField permanantCodeField;
    private JLabel permanantCodeLabel;
    private JRadioButton maleRadio;
    private JRadioButton femaleRadio;
    private ButtonGroup genderGroup;
    
    class Expertise{
        private int id;
        private String text;
        public Expertise(int id, String text){
            this.id=id;
            this.text=text;
            
        }
        public String toString(){
           return  this.text;
        }
        public int getID(){
           return  this.id;
        }
    }
}
